package com.uinnova.product.eam.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.binary.core.exception.BinaryException;
import com.uinnova.product.eam.constant.MeasureFlowState;
import com.uinnova.product.eam.dao.MeasureFlowRecordDao;
import com.uinnova.product.eam.model.es.MeasureFlowRecord;
import com.uinnova.product.eam.service.MeasureFlowRecordService;
import com.uinnova.product.eam.vo.MeasureFlowRecordSaveVo;
import com.uino.dao.util.ESUtil;
import lombok.extern.log4j.Log4j2;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
@Log4j2
public class MeasureFlowRecordServiceImpl implements MeasureFlowRecordService {

    @Autowired
    private MeasureFlowRecordDao measureFlowRecordDao;

    @Override
    public void save(MeasureFlowRecordSaveVo saveVo) {
        switch (saveVo.getAction()) {
            case 1:
                doSaveCaseFLowCreate(saveVo);
                break;
            case 2:
                doSaveCasePMORejectInit(saveVo);
                break;
            case 3:
                doSaveCaseMeasure(saveVo);
                break;
            default:
                throw new BinaryException("不支持的动作");
        }
    }

    private void doSaveCaseFLowCreate(MeasureFlowRecordSaveVo saveVo) {
        MeasureFlowRecord record = new MeasureFlowRecord();
        record.setId(ESUtil.getUUID());
        record.setArchReviewId(saveVo.getArchReviewId());
        record.setFlowBatch(1);
        record.setState(MeasureFlowState.NOT_YEAT);
        record.setCreateTime(ESUtil.getNumberDateTime());
        record.setModifyTime(ESUtil.getNumberDateTime());
        measureFlowRecordDao.saveOrUpdate(record);
    }

    private void doSaveCasePMORejectInit(MeasureFlowRecordSaveVo saveVo) {
        Integer dbMaxFlowBatch = getMaxFlowBatch(saveVo.getArchReviewId());
        MeasureFlowRecord record = new MeasureFlowRecord();
        record.setId(ESUtil.getUUID());
        record.setArchReviewId(saveVo.getArchReviewId());
        record.setFlowBatch(dbMaxFlowBatch + 1);
        record.setState(MeasureFlowState.NOT_YEAT);
        record.setCreateTime(ESUtil.getNumberDateTime());
        record.setModifyTime(ESUtil.getNumberDateTime());
        measureFlowRecordDao.saveOrUpdate(record);
    }

    private void doSaveCaseMeasure(MeasureFlowRecordSaveVo saveVo) {
        List<MeasureFlowRecord> flowRecords = queryByArchReviewId(saveVo.getArchReviewId());
        if (CollectionUtils.isEmpty(flowRecords)) {
            log.error("无满足条件流程度量记录：{}", JSONObject.toJSONString(saveVo));
            return;
        }
        //是否从第一个流程批次开始：如果不是的话，说明是旧版流程，不支持测评
        BoolQueryBuilder query = QueryBuilders.boolQuery();
        query.must(QueryBuilders.termQuery("archReviewId", saveVo.getArchReviewId()));
        query.must(QueryBuilders.termQuery("flowBatch", 1));
        List<MeasureFlowRecord> batchFlow1 = measureFlowRecordDao.getListByQuery(query);

        MeasureFlowRecord record = flowRecords.get(0);
        record.setState(saveVo.getMeasureConfId().equals(-1L) || CollectionUtils.isEmpty(batchFlow1) ?
                MeasureFlowState.NOT_NEED : MeasureFlowState.MEASURE);
        record.setModifyTime(ESUtil.getNumberDateTime());
        record.setMeasureConfId(saveVo.getMeasureConfId());
        measureFlowRecordDao.saveOrUpdate(record);
    }

    private Integer getMaxFlowBatch(Long archReviewId) {
        List<MeasureFlowRecord> flowRecords = queryByArchReviewId(archReviewId);
        return CollectionUtils.isEmpty(flowRecords) ? 1 : flowRecords.get(0).getFlowBatch();
    }

    public List<MeasureFlowRecord> queryByArchReviewId(Long archReviewId) {
        TermQueryBuilder query = QueryBuilders.termQuery("archReviewId", archReviewId);
        return measureFlowRecordDao.getSortListByQuery(1, 3000, query, "createTime", false).getData();
    }

    @Override
    public MeasureFlowRecord queryByArchReviewId(Long archReviewId, Integer flowBatch) {
        BoolQueryBuilder query = QueryBuilders.boolQuery();
        query.must(QueryBuilders.termQuery("archReviewId", archReviewId));
        query.must(QueryBuilders.termQuery("flowBatch", flowBatch));
        List<MeasureFlowRecord> records = measureFlowRecordDao.getListByQuery(query);
        return CollectionUtils.isEmpty(records) ? null : records.get(0);
    }
}
